package com.mipo.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mipo.dao.imp.EmployeeDAO;
import com.mipo.dao.imp.EmployeeDAOImp;
import com.mipo.dao.imp.MessageDAO;
import com.mipo.dao.imp.MessageDAOImp;
import com.mipo.dao.imp.MessageReceiveDAO;
import com.mipo.dao.imp.MessageReceiveDAOImp;
import com.mipo.vo.Employee;
import com.mipo.vo.Message;
import com.mipo.vo.MessageReceive;

public class SendReplyService extends HttpServlet {

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		Employee emp = (Employee) session.getAttribute("tzx");
		String title = request.getParameter("title");
		String contexts = request.getParameter("contexts");
		String receiveId = request.getParameter("receiveId");
		String riceiveName = request.getParameter("riceiveName");
		Message m = new Message();
		m.setTitle(title);
		m.setContexts(contexts);
		m.setSendId(emp.getEmpId());
		m.setSendName(emp.getEmpName());
		m.setSendTime(String.valueOf(new Date().toLocaleString()));
		MessageDAO mdao = new MessageDAOImp();
		
		try {
			boolean b = mdao.insertMessage(m);
			if (b) {
				int mid = mdao.queryMessageId();
				EmployeeDAO edao = new EmployeeDAOImp();
					MessageReceive mr = new MessageReceive();
					mr.setMid(mid);
					mr.setReceiveId(Integer.valueOf(receiveId));
					mr.setRiceiveName(riceiveName);
					mr.setSeeTime(null);
					mr.setState(0);
					MessageReceiveDAO rdao = new MessageReceiveDAOImp();
					boolean bmr = rdao.insertMessageReceive(mr);
			}
			response.sendRedirect("inTake.shtml");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
